//package com.example.dubbo.service.business.excption;
//
//import com.example.common.vo.R;
//import com.google.common.base.Throwables;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.stereotype.Component;
//
///**
// * @author NiJo_H
// * @version 1.0
// * @date 2020/3/19 10:50
// */
//@Component
//@Aspect
//@Slf4j
//public class ServiceExceptionHandler {
//
//    /**
//     * 过滤拦截返回R<T>接口
//     */
//    @Pointcut(value = "execution(* com.example.dubbo.service.business.impl.demo.BusinessServiceImpl.handleBusiness(..))")
//    private void servicePointcut() {
//    }
//
//
////    /**
////     * 持有@GlobalTransactional方法
////     */
////    @Pointcut(value = "@annotation(io.seata.spring.annotation.GlobalTransactional)")
////    private void transactionalPointcut() {
////    }
//
//    /**
//     * 异常处理切面 将异常包装为R<T>，避免dubbo进行包装
//     *
//     * @param pjp 处理点
//     * @return Object
//     */
////    @Around("servicePointcut()&& !transactionalPointcut()")
////    public Object doAround(ProceedingJoinPoint pjp) {
////        Object[] args = pjp.getArgs();
////        try {
////            return pjp.proceed();
////        } catch (Exception e) {
////            processException(pjp, args, e);
////            return R.failed("exception occured on dubbo service side: " + e.getMessage());
////        } catch (Throwable throwable) {
////            processException(pjp, args, throwable);
////            return R.failed("exception occured on dubbo service side: " + throwable.getMessage());
////        }
////    }
//
//    /**
//     * 任何持有@GlobalTransactional注解的方法异常处理切面 将自定义的业务异常转为RuntimeException:
//     * 1.规避dubbo的包装，让customer可以正常获取message
//     * 2.抛出RuntimeException使事务可以正确回滚 其他异常不处理
//     *
//     * @param pjp 处理点
//     * @return R<T>
//     */
//    @AfterReturning("servicePointcut()")
//    public Object doTransactionalAround(JoinPoint pjp) throws Throwable {
//        try {
//            return R.failed("123");
//        } catch (Exception e) {
////            Object[] args = pjp.getArgs();
////            processException(pjp, args, e);
//            return R.failed(e.getMessage());
//        }
//    }
//
//
//    /**
//     * 处理异常
//     *
//     * @param joinPoint 切点
//     * @param args      参数
//     * @param throwable 异常
//     */
//    private void processException(final ProceedingJoinPoint joinPoint, final Object[] args, Throwable throwable) {
//        String inputParam = "";
//        if (args != null && args.length > 0) {
//            StringBuilder sb = new StringBuilder();
//            for (Object arg : args) {
//                sb.append(",");
//                sb.append(arg);
//            }
//            inputParam = sb.toString().substring(1);
//        }
//        log.error("方法: {},入参: {},错误信息: {}", joinPoint.toLongString(), inputParam,
//                Throwables.getStackTraceAsString(throwable));
//    }
//}
